package net.thxjj.mobileServer.controller.mobile;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.thxjj.mobileServer.controller.base.BaseController;
import net.thxjj.mobileServer.entity.*;
import net.thxjj.mobileServer.service.IUserService;
import net.thxjj.mobileServer.util.ParameterMap;
import net.thxjj.mobileServer.util.SHA;
import net.thxjj.mobileServer.util.Tools;
import net.thxjj.sitermodel.Device;
import net.thxjj.sitermodel.MobileUser;
import net.thxjj.sitermodel.Place;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @ClassName:MobileUserController
 * @Description:TODO
 * @Autor:TracyHenry
 * @Date:2022/12/18 0018
 * @Version:1.0
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class MobileUserController extends BaseController {



    @Autowired
    private IUserService userService;



    /**
     * changePassword
     * @return
     * 手机端修改密码接口
     */
    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    @ResponseBody
    public Object changePassword(@RequestBody JSONObject jsonObject){

        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        String psw  = jsonObject.getString("oldPassword");
        String new_code = jsonObject.getString("newPassword");

        if(Tools.isEmpty(psw)){
            return ResponseModel.getModel(ResultEnum.OLD_PASSWORD_IS_EMPTY,null);
        }else if(Tools.isEmpty(new_code)){
            return ResponseModel.getModel(ResultEnum.NEW_PASSWORD_IS_EMPTY,null);
        }



        try {
            psw = SHA.encryptSHA(psw);
            new_code = SHA.encryptSHA(new_code);
        } catch (Exception e) {

            e.printStackTrace();
        }

        ParameterMap pm = new ParameterMap();
        pm.put("user_id",user_id);
        pm.put("password",psw);
        pm.put("password_new",new_code);

        return userService.changePassword(pm);
    }

    /**
     * getDeviceList
     * @return
     * 移动端获取设备列表接口
     */
    @RequestMapping(value = "/deviceList",method = RequestMethod.GET)
    @ResponseBody
    public Object getDeviceList(){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

      List<Device> list = userService.getBindDeviceList(user_id);


        return ResponseModel.getModel(ResultEnum.SUCCESS,list);
    }


    /**
     * getInfo
     * @return
     * 获取用户信息
     */
    @RequestMapping(value = "/getInfo",method = RequestMethod.GET)
    @ResponseBody
    public Object getInfo(){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        MobileUser parameterMap = userService.getUserInfoById(user_id);


        return ResponseModel.getModel(ResultEnum.SUCCESS,parameterMap);

    }


    /**
     * bindPushTag
     * @return
     * 用户绑定FCM推送
     */
    @RequestMapping(value = "/bindPushTag",method = RequestMethod.POST)
    @ResponseBody
    public Object bindPushTag(){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        return null;
    }

    /**
     * unbindPushTag
     * @return
     * 用户解绑FCM推送
     */
    @RequestMapping(value = "/unbindPushTag",method = RequestMethod.POST)
    @ResponseBody
    public Object unbindPushTag(){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        return null;
    }

    /**
     * cancelAccunt
     * @return
     * 移动端用户注销账号
     */
    @RequestMapping(value = "/cancelAccunt",method = RequestMethod.POST)
    @ResponseBody
    public Object cancelAccunt(){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");
        return userService.cancelAccount(user_id);
    }

    /**
     * bindDevice
     * @return
     * 用户绑定设备
     */
    @RequestMapping(value = "/bindDevice",method = RequestMethod.POST)
    @ResponseBody
    public Object bindDevice(@RequestBody JSONObject jsonObject){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        String devTid = jsonObject.getString("devTid");
        String bindKey = jsonObject.getString("bindKey");

        return userService.insertRelation(devTid,bindKey,user_id);
    }


    /**
     * unbindDevice
     * @return
     * 用户解绑设备（包括分享设备）
     */
    @RequestMapping(value = "/unbindDevice",method = RequestMethod.POST)
    @ResponseBody
    public Object unbindDevice(@RequestBody JSONObject jsonObject){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        String devTid = jsonObject.getString("devTid");
        String bindKey = jsonObject.getString("bindKey");

        return userService.unbind(devTid,bindKey,user_id);
    }


    /**
     * getNewDeviceList
     *
     * 获取用户下新添加的设备
     */
    @RequestMapping(value = "/getNewDeviceList",method = RequestMethod.POST)
    public Object getNewDeviceList(@RequestBody JSONObject jsonObject){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        String pinCode = jsonObject.getString("pinCode");

        if(Tools.isEmpty(pinCode)){
            return ResponseModel.getModel(ResultEnum.PARAMS_EMPTY_ERROR,null);
        }

        return userService.getNewDeviceList(pinCode,user_id);
    }


    /**
     * getPlacesAndRooms
     * @return
     * 用户获取地点以及房间相关信息
     */
    @RequestMapping(value = "/getPlacesAndRooms",method = RequestMethod.GET)
    @ResponseBody
    public Object getPlacesAndRooms(){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        List<Place> list = userService.getPlaceAndRooms(user_id);

        return ResponseModel.getModel(ResultEnum.SUCCESS,list);
    }

    /**
     * getPinCode
     *
     * 用户获取pincode 用于配网
     */
    @RequestMapping(value = "/getPinCode",method = RequestMethod.POST)
    public Object getPinCode(@RequestBody JSONObject jsonObject){
        long user_id = (long)this.getRequest().getSession().getAttribute("user_id");

        String ssid = jsonObject.getString("ssid");

        if(Tools.isEmpty(ssid)){
            return ResponseModel.getModel(ResultEnum.PARAMS_EMPTY_ERROR,null);
        }

        return userService.getPinCode(user_id,ssid);
    }

}
